home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 17 / AMIGAplus Sonderheft 17 (1999)(ICP)(DE)[!].iso / dsound / source.lha / PlayStereo.c < prev   
C/C++ Source or Header  |  1994-07-18  |  6KB  |  219 lines

  1.  
  2. /*************************************************************************/
  3. /*                  PlayStereo.c                 */
  4. /*        Contains code used to play stereo samples         */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17. #include "minrexx.h"
  18.  
  19. #include <proto/intuition.h>
  20. #include <proto/exec.h>
  21. #include <proto/dos.h>
  22.  
  23. extern UBYTE rightAMap[];
  24. extern UBYTE leftAMap[];
  25. extern UBYTE eitherAMap[];
  26. extern UBYTE bothAMap[];
  27.  
  28. extern UBYTE volume;
  29. extern UWORD speed;
  30. extern ULONG bufSize;
  31.  
  32. extern BOOL readAll;
  33. extern BOOL loop;
  34. extern struct Window *window;
  35.  
  36. extern ULONG signalMask;
  37. extern BOOL rexxAbort;
  38.  
  39. /*Play a stereo sample out of both speakers*/
  40. /*If the user specifies that just one of the two stereo channels will*/
  41. /*be played, DSound.c calls playMonoSample*/
  42.  
  43. void playStereoSample(BPTR leftFile,channel audioChannel,
  44.               struct Voice8Header *vhdr, ULONG length, char *filename)
  45. {
  46.    struct IOAudio *iob1_right,*iob2_right,*iob1_left,*iob2_left;
  47.    struct IOAudio *cur_right,*cur_left,*alt_right,*alt_left;
  48.    extern long arexxSigBit;
  49.    ULONG toRead;
  50.    ULONG amountLeft;
  51.    ULONG sampleSize=length;
  52.    BOOL done=FALSE;
  53.    BPTR rightFile;
  54.  
  55.    /*Open the file again*/
  56.    rightFile=dupFileHandle(leftFile,filename);
  57.  
  58.    /*And position ourselves at the start of the right channel's data*/
  59.    Seek(rightFile,length,OFFSET_CURRENT);
  60.  
  61.    /*Read the entire sample into memory, if specified*/
  62.    if(readAll)
  63.    {
  64.       storeLeft(leftFile,length,bufSize);
  65.       storeRight(rightFile,length,bufSize);
  66.       Close(rightFile);
  67.       leftFile=0L;
  68.       rightFile=4L;
  69.    }
  70.  
  71.    /*Get the first audio channel*/
  72.    iob1_left=GetAudioChannel(bufSize,leftAMap);
  73.    if(iob1_left==NULL)
  74.    {
  75.       WriteMsg("Couldn't create the first stereo buffer\n");
  76.       cleanup(150);
  77.    }
  78.  
  79.    iob1_right=GetAudioChannel(bufSize,rightAMap);
  80.    if(iob1_right==NULL)
  81.    {
  82.       WriteMsg("Couldn't create the second stereo buffer\n");
  83.       cleanup(150);
  84.    }
  85.  
  86.    /* If the user didn't specify a volume, get it from the VHDR */
  87.    if(volume==0)
  88.       volume=(vhdr->volume>>10);
  89.  
  90.    /* If the VHDR gave a volume of zero, use maximum volume*/
  91.    if(volume==0)
  92.       volume=64;
  93.  
  94.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  95.    /* rate found in the VHDR) */
  96.    if(speed==0)
  97.       speed=1000000000/(vhdr->samplesPerSec*279);
  98.    else
  99.       speed=1000000000/(speed*279);
  100.  
  101.    InitAudioChannel(iob1_left,volume,speed);
  102.    InitAudioChannel(iob1_right,volume,speed);
  103.  
  104.    /*Get the 2nd audio channel*/
  105.    iob2_left=DuplicateAudioChannel(iob1_left);
  106.  
  107.    if(iob2_left==NULL)
  108.    {
  109.       FreeAudioChannel(iob1_left);
  110.       FreeAudioChannel(iob1_right);
  111.       WriteMsg("Couldn't create the second buffer");
  112.       cleanup(175);
  113.    }
  114.  
  115.    iob2_right=DuplicateAudioChannel(iob1_right);
  116.    if(iob2_right==NULL)
  117.    {
  118.       FreeAudioChannel(iob1_left);
  119.       DeleteDuplication(iob2_left);
  120.       FreeAudioChannel(iob1_right);
  121.       WriteMsg("Couldn't create the second buffer");
  122.       cleanup(175);
  123.    }
  124.  
  125.    /* Load the first buffer*/
  126.    toRead=MIN(length,bufSize);
  127.    LoadAudioBuffer(leftFile,iob1_left,toRead);
  128.    LoadAudioBuffer(rightFile,iob1_right,toRead);
  129.    iob1_left->ioa_Length=iob1_right->ioa_Length=toRead;
  130.  
  131.    length-=toRead;
  132.    if(length==0 && loop)
  133.    {
  134.       length=sampleSize;
  135.       if(!readAll)
  136.       {
  137.      Seek(leftFile,-sampleSize,OFFSET_CURRENT);
  138.      Seek(rightFile,-sampleSize,OFFSET_CURRENT);
  139.       }
  140.    }
  141.  
  142.    /*Initialize the sample position info*/
  143.    updateSampleInfo(0,sampleSize,vhdr->samplesPerSec);
  144.  
  145.    /*And queue up the play requests*/
  146.    BeginIO((struct IORequest *)iob1_left);
  147.    BeginIO((struct IORequest *)iob1_right);
  148.  
  149.    cur_right=iob2_right;
  150.    cur_left=iob2_left;
  151.    alt_right=iob1_right;
  152.    alt_left=iob1_left;
  153.  
  154.    /*Loop while there's stuff to read*/
  155.    while(!done && !rexxAbort)
  156.    {
  157.       toRead=MIN(length,bufSize);
  158.  
  159.       if(toRead!=0)
  160.       {
  161.      LoadAudioBuffer(leftFile,cur_left,toRead);
  162.      LoadAudioBuffer(rightFile,cur_right,toRead);
  163.      cur_left->ioa_Length=cur_right->ioa_Length=toRead;
  164.      BeginIO((struct IORequest *)cur_left);
  165.      BeginIO((struct IORequest *)cur_right);
  166.      amountLeft=length-=toRead;
  167.  
  168.      if(length==0 && loop)
  169.      {
  170.         length=sampleSize;
  171.         if(!readAll)
  172.         {
  173.            Seek(leftFile,-sampleSize,OFFSET_CURRENT);
  174.            Seek(rightFile,-sampleSize,OFFSET_CURRENT);
  175.         }
  176.      }
  177.      done=FALSE;
  178.       }
  179.       else
  180.      done=TRUE;
  181.  
  182.       /*Wait for the buffer to finish*/
  183.       if((Wait(1<<cur_right->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit |
  184.        signalMask | arexxSigBit) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  185.      done=TRUE;
  186.  
  187.       dispRexxPort();
  188.       /*Update the sample position info*/
  189.       updateSampleInfo(sampleSize-amountLeft-toRead,sampleSize,vhdr->samplesPerSec);
  190.  
  191.       /*If we got a message from the window, it is a CLOSEWINDOW message*/
  192.       /*and we're done*/
  193.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  194.       {
  195.      done=TRUE;
  196.       }
  197.  
  198.       swapPointers(&cur_left,&alt_left);
  199.       swapPointers(&cur_right,&alt_right);
  200.    }
  201.  
  202.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  203.    /*how much memory to free*/
  204.    iob1_left->ioa_Length=iob2_left->ioa_Length=bufSize;
  205.    iob1_right->ioa_Length=iob2_right->ioa_Length=bufSize;
  206.  
  207.    FreeAudioChannel(iob1_left);
  208.    DeleteDuplication(iob2_left);
  209.    FreeAudioChannel(iob1_right);
  210.    DeleteDuplication(iob2_right);
  211.  
  212.    if(rightFile != 4L)
  213.       Close(rightFile);
  214.  
  215.    return;
  216. }
  217.  
  218. /*End of PlayStereo.c*/
  219.